package logic.common.game;

import fileHandling.ModelImporter;
import gameStates.absGamesStates.AbsIngameState;

import interfaces.hud.BasicHUD;
import interfaces.hud.topWindows.MotherShipsStatusHUD;

import org.fenggui.StandardWidget;

import settings.GraphicSettings;

import logic.common.game.endConditions.MotherShipCondition;
import logic.common.game.endConditions.TimeOutCondition;
import logic.common.missions.DestroyMission;
import logic.common.missions.DroneHarassMission;
import logic.common.missions.MissionFactory;
import logic.common.team.Team;
import logic.ships.mothership.MotherShip;

public class DestructionSetup extends GameSetup {
	
	public DestructionSetup(Game game) {
		super(game);
	}
	
	@Override
	public void setupGame(AbsIngameState ingameState) {
		initAI();
		
		Team teamA = game.getTeamA();
		Team teamB = game.getTeamB();
		
		addDrones(teamA, ingameState);
		addDrones(teamB, ingameState);
		
		attachMotherShips(ingameState);
		attachSpawnStations(ingameState);
		
		initTeamControllers(ingameState);

		preloadModels(teamA);
		preloadModels(teamB);
	}
	
	protected void preloadModels(Team team) {
		preloadModels(team.getNewFrigateProperties().getModelPath());
		preloadModels(team.getNewCarrierProperties().getModelPath());
	}
	
	protected void preloadModels(String modelPath) {
		String quality = GraphicSettings.get().getGeometryQuality();
		while(quality != null) {
			ModelImporter.getModel(modelPath, quality);
			quality = ModelImporter.nextLevel(quality, true);
		}
	}
	
	@Override
	public void addStandardMissions(final Team team, final Team enemyTeam) {
		MotherShip enemyMS = enemyTeam.getMotherShip();
		DestroyMission mission = MissionFactory.createDestroyMission(team, enemyMS);
		team.addMission(mission);
		
		DroneHarassMission droneMission = MissionFactory.createDroneHarassMission(team, enemyMS);
		team.addMission(droneMission);
		
		initSpawnStationMissions(team, enemyTeam);
	}

	@Override
	public void initEndConditions() {
		new TimeOutCondition(game);
		new MotherShipCondition(game);
	}
	
	@Override
	public StandardWidget getTopHUDElement(BasicHUD hud) {
		MotherShip teamMS = game.getCurrentPlayer().getTeam().getMotherShip();
		MotherShip enemyMS = game.getCurrentPlayer().getEnemyTeam().getMotherShip();
		return new MotherShipsStatusHUD(teamMS, enemyMS, hud);
	}
	
	@Override
	public boolean hasMissions() { return true; }
	
	@Override
	public boolean hasMotherShips() { return true; }
	
	@Override
	public boolean hasTeamStatusWindow() { return true; }
}